#include "mqtt_service.h"
#include "power_utils.h"
#include "lx_serial.h"
#include <pthread.h>

int server_num = 0;
MQTTServer servers[10];
extern t_RunCfg runCfg;

int publish_data(char *ip, int port, char *username, char *password, char *topic, char *payload) {
    //mosquitto_pub -h '47.109.37.185' -p 11883 -t 'bbbbb' -u 'iot' -P 'iot20251231' -m '123123123123'
    uint8_t cmd[4096] = {0};
    sprintf(cmd, "mosquitto_pub -h '%s' -p %d -u '%s' -P '%s'  -t '%s' -m '%s'",\
            ip, port, username, password, topic, payload);

    system(cmd);
    return 0;
}
/**
 * 发送数据到MQTT服务器
 */
int mqtt_send_data(char *payload){
    for (size_t i = 0; i < server_num; i++)
    {
        if (servers[i].enable)
        {
            publish_data(servers[i].host, servers[i].port, servers[i].username, servers[i].password, servers[i].pubtopic, payload);
        }
        
    }
    
}


